define(function (require,exports,module) {
    var sTpl = require("./aircraftPreloading.html");
    require("../../lib/fullcalendar/moment.min.js");
    require("./aircraftPreloading.css");
    var routeRefresh=function ($this,init) {
        if(init){
            var form={
                startDate:'',
                endDate:'',
                flightNum:''
            }
            if($this.$route.params.type=='today'){
                form.startDate = new Date().format('yyyy-MM-dd');
                form.endDate = new Date().format('yyyy-MM-dd');
            }else{
                if($this.$route.params.type=='mediumAndLong'){
                    var startDate = new Date(new Date().getTime()+1000*3600*24*7);
                    var endDate = new Date(new Date().getTime()+1000*3600*24*15);
                    form.startDate = startDate.format('yyyy-MM-dd');
                    form.endDate = endDate.format('yyyy-MM-dd');
                }else{
                    if($this.$route.params.type=='normal'){
                        var startDate = new Date();
                        var endDate = new Date(new Date().getTime()+1000*3600*24*7);
                        form.startDate = startDate.format('yyyy-MM-dd');
                        form.endDate = endDate.format('yyyy-MM-dd');
                    }
                }
            }
            form.url=basepath+"/api/flightPlane/queryAirPreloading?sort=expectDate&startDate="+form.startDate+"&endDate="+form.endDate+"&flightNum="+(form.flightNum?form.flightNum:"")
            return form;
        }else{
            if($this.$route.params.type=='today'){
                $this.form.startDate = new Date().format('yyyy-MM-dd');
                $this.form.endDate = new Date().format('yyyy-MM-dd')

            }else{
                if($this.$route.params.type=='mediumAndLong'){
                    var startDate = new Date(new Date().getTime()+1000*3600*24*7);
                    var endDate = new Date(new Date().getTime()+1000*3600*24*15);
                    $this.form.startDate = startDate.format('yyyy-MM-dd');
                    $this.form.endDate = endDate.format('yyyy-MM-dd');
                }else{
                    if($this.$route.params.type=='normal'){
                        var startDate = new Date();
                        var endDate = new Date(new Date().getTime()+1000*3600*24*7);
                        $this.form.startDate = startDate.format('yyyy-MM-dd');
                        $this.form.endDate = endDate.format('yyyy-MM-dd');
                    }
                }

            }
            $this.url=basepath+"/api/flightPlane/queryAirPreloading?sort=expectDate&startDate="+$this.form.startDate+"&endDate="+$this.form.endDate+"&flightNum="+($this.form.flightNum?$this.form.flightNum:"")
        }
    }

    var VueComponent = Vue.extend({
        template: sTpl,
        data:function () {
            var form=routeRefresh(this,true);

            var checkInt = function(rule, value, callback){
                setTimeout(function()  {
                    if (!Number.isInteger(value)) {
                    callback(new Error('请输入合法数字'));
                } else {
                    if (value < 0) {
                        callback(new Error('请输入合法数字'));
                    } else {
                        callback();
                    }
                }
            }, 1000);
            };
            var checkDouble = function(rule, value, callback){
                setTimeout(function()  {

                    if(typeof value !='number'){
                        callback(new Error('请输入合法数字'));
                    }else {
                        if (value < 0) {
                            callback(new Error('请输入合法数字'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);
            };
            return {
                rules: {
                    precheckNum: [
                        { validator: checkInt, trigger: 'blur' }
                    ],
                    adultNum: [
                        { validator: checkInt , trigger: 'blur' }
                    ],
                    childrenNum: [
                        { validator: checkInt , trigger: 'blur' }
                    ],
                    babyNum:[
                        { validator: checkInt , trigger: 'blur' }
                    ],
                    unattendedNum:[
                        { validator: checkInt , trigger: 'blur' }
                    ],
                    wheelchairNum:[
                        { validator: checkInt , trigger: 'blur' }
                    ],
                    other:[
                        { validator: checkInt , trigger: 'blur' }
                    ],
                    totalPeople:[
                        { validator: checkInt , trigger: 'blur' }
                    ],
                    /*Double*/
                    cargoWeight:[
                        { validator: checkDouble , trigger: 'blur' }
                    ],
                    mailWeight:[
                        { validator: checkDouble , trigger: 'blur' }
                    ],
                    baggageWeight:[
                        { validator: checkDouble, trigger: 'blur' }
                    ],
                    courierNumber:[
                        { validator: checkInt , trigger: 'blur' }
                    ],
                    largeCargoNum:[
                        { validator: checkInt , trigger: 'blur' }
                    ],
                    totalBaggageNum:[
                        { validator: checkInt , trigger: 'blur' }
                    ],
                    animalNum:[
                        { validator: checkInt , trigger: 'blur' }
                    ]
                },
                title:'',
                isee:false,
                tableDataIndex:'',
                dialogFormVisible1:false,
                originating:'',
                isLoading:'',
                destination:'',
                stowageFormInline:{
                    precheckNum:'' ,
                    adultNum:'',
                    childrenNum:'',
                    babyNum:'',
                    unattendedNum:'',
                    wheelchairNum:'',
                    other:'',
                    totalPeople:'',
                    cargoWeight:'',
                    mailWeight:'',
                    baggageWeight:'',
                    courierNumber:'',
                    largeCargoNum:'',
                    totalBaggageNum:'',
                    animalNum:'',
                    loadingDate:''
                },
                tableData: {},
                flightInfo:[],
                form:form,
                planes:[],
                url:form.url,
                preUrl:form.url,

            }
        },
        methods:{
            see:function () {
               this.isee=!this.isee;
            },
            clear:function(){
                this.$refs.flightPlane.dialogFormVisible = false;
            },
            planLoad:function(scope){
                this.isee=false;
                this.tableDataIndex=scope.$index;
                this.flightPlanePo=JSON.parse(JSON.stringify(scope.row));
                if(this.flightPlanePo.industryLoadPo==null){
                    this.stowageFormInline={
                        precheckNum:'',
                            adultNum:'',
                            childrenNum:'',
                            babyNum:'',
                            unattendedNum:'',
                            wheelchairNum:'',
                            other:'',
                            totalPeople:'',
                            cargoWeight:'',
                            mailWeight:'',
                            baggageWeight:'',
                            courierNumber:'',
                            largeCargoNum:'',
                            totalBaggageNum:'',
                            animalNum:'',
                            loadingDate:''
                    };
                }else{
                    this.stowageFormInline=this.flightPlanePo.industryLoadPo;
                }
                this.title= "航班号："+this.flightPlanePo.flightInfo.flightNum;
                this.$refs.flightPlane.dialogFormVisible = true;
            },
            save:function(){
                this.flightPlanePo.industryLoadPo=this.stowageFormInline;
                var $this = this;
                this.$refs.stowageFormInline.validate(function(validate){
                    if(validate){
                        axios.post(basepath+'/api/aircraftPreloading/saveIndustryLoadPo',$this.flightPlanePo)
                            .then(function (response) {
                                $this.$notify({
                                    title: '保存成功',
                                    message: '保存成功'
                                });
                               $this.tableData[$this.tableDataIndex].industryLoadPo=response.data;
                                $this.$refs.flightPlane.dialogFormVisible = false;

                            });

                    }else{
                        $this.$notify({
                            title: '保存失败',
                            message: '保存失败'
                        });
                        return  false;
                    }
                });



            },

            planLoadedDetails:function(scope) {
                this.flightPlanePo=JSON.parse(JSON.stringify(scope.row));
                this.flightInfo=scope.row.flightInfo;
                this.stowageFormInline=scope.row.industryLoadPo;
                if(this.flightPlanePo.industryLoadPo==null) {
                    this.stowageFormInline = {
                        precheckNum: '',
                        adultNum: '',
                        childrenNum: '',
                        babyNum: '',
                        unattendedNum: '',
                        wheelchairNum: '',
                        other: '',
                        totalPeople: '',
                        cargoWeight: '',
                        mailWeight: '',
                        baggageWeight: '',
                        courierNumber: '',
                        largeCargoNum: '',
                        totalBaggageNum: '',
                        animalNum: '',
                        loadingDate: ''
                    }
                }
                this.originating=scope.row.originating;
                this.destination=scope.row.destination;
                this.dialogFormVisible1=true;
            },


            queryFlightPlane:function () {
                var $this = this;
                axios.get(basepath+"/api/flightPlane/queryAirPreloading?sort=expectDate&startDate="+(this.form.startDate?this.form.startDate:'')
                    +"&endDate="+(this.form.endDate?this.form.endDate:'')+"&flightNum="+(this.form.flightNum?this.form.flightNum:"")).then(function (response) {
                    $this.tableData=response.data;
                });

            },
            handleSelect:function (val) {
                this.form.flightNum=val.flightNum;
            },
        },
        watch:{
            '$route.params.type':{
                handler:function(val,oldval){
                    routeRefresh(this,false);
                },
                deep:true
            }
        },
        created:function () {
            var $this=this;
            axios.get("/dam/api/flightInfo/queryFlightInfo?filter=").then(function (response) {
                $this.planes=response.data;
            })
            this.queryFlightPlane();
        },
        activated:function () {
            messages.$emit('change','飞机预配载');
        }
    })
    module.exports=VueComponent;
    module.exports.reDefine=function (val) {
        module.exports.type=val;
        return module.exports;
    }

})
